package dasom.basic;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SungJukV3
 */
@WebServlet("/SungJukV3.do")
public class SungJukV3 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		// 서버 응답 종류 지정
		res.setContentType("text/html;charset=UTF-8");
		//출력스트림 지정(서버를 출발지, 브라우저를 목적지로 하는 스트림 지정)
		PrintWriter out=res.getWriter();
		//브라우저로 출력할 내용 작성
		out.print("<html>");
		out.print("<head><title>서블릿으로 만드는 성적처리프로그램 v3");
		out.print("</title></head>");
		out.print("<body><h1>서블릿으로 만드는 성적처리프로그램 v3</h1>");
		out.print("<form name='sj' method='get'>");
		out.print("<div>이름<input type='text'name='name'></div>");
		out.print("<div>국어<input type='text'name='kor'></div>");
		out.print("<div>영어<input type='text'name='eng'></div>");
		out.print("<div>수학<input type='text'name='mat'></div>");
		out.print("<div><button type='submit'>입력완료</button>");
		out.print("<button type='reset'>다시입력</button></div>");
		out.print("</form>");
		out.print("</body>");
		out.print("</html>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		res.setContentType("text/html;charset=UTF-8");
		PrintWriter out = res.getWriter();
		
		//요청 객체에 대해 한글처리
		req.setCharacterEncoding("UTF-8");
		
		//비지니스 로직 처리(총점,평균,학점 계산)
		String name = req.getParameter("name");
		int kor = Integer.parseInt(req.getParameter("kor"));
		int eng = Integer.parseInt(req.getParameter("eng"));
		int mat = Integer.parseInt(req.getParameter("mat"));
		int tot = Integer.parseInt(req.getParameter("kor"))+Integer.parseInt(req.getParameter("eng"))+Integer.parseInt(req.getParameter("mat"));
		float avg = (float)tot/3;
		char grd='가';
		switch((int)avg/10){
		case 10:case 9:grd='수';break;
		case 8:        grd='우';break;
		case 7:        grd='미';break;
		case 6:        grd='양';
		}
	
		
		//성적 처리 결과 출력
		out.print("<html>");
		out.print("<head><title>성적 처리 결과</title></head>");
		out.print("<body>");
		out.print("<h1>성적 처리 결과</h1>");
		out.print("<p> 이름: "+name+"<br/>");
		out.print("<p> 국어: "+kor+"<br/>");
		out.print("<p> 영어: "+eng+"<br/>");
		out.print("<p> 수학: "+mat+"<br/>");
		out.print("<p> 총점: "+tot+"<br/>");
		out.print("<p> 평균: "+avg+"<br/>");
		out.print("<p> 학점: "+grd+"<br></p>");
		out.print("</body>");
		out.print("</html>");
		
	}

}
